package com.common.action;

import java.util.List;
import java.util.Set;

import com.common.bean2.T2;
import com.common.bean2.T3;

public class TestManyToOne extends AbstractTestAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	T2 t2;
	
	T3 t3;
	
	Long id;
	
	List<T2> ttl;
	
	Set<T2> t2s;
	
	List<T3> t3l;

	public List<T3> getT3l() {
		return t3l;
	}

	public void setT3l(List<T3> t3l) {
		this.t3l = t3l;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addObj() throws Exception{
		t2.setT3(t3);
		testService.addObject(t3);
		testService.addObject(t2);
		return SUCCESS;
	}
	
	public String addObjList() throws Exception{
		//cascade=none or other
		testService.addObject(t3);
		for(T2 tt:ttl){
			tt.setT3(t3);
			testService.addObject(tt);
		}
		id = t3.getId();
		return SUCCESS;
	}
	
	public String updateT2s(){
		testService.updateObject(t3);
		for(T2 tt:ttl){
			tt.setT3(t3);
			testService.updateObject(tt);
		}
		id = t3.getId();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String findAllT3() throws Exception{
		t3l = (List<T3>)testService.findAll(T3.class);
		getRequest().setAttribute("t3list", t3l);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String findAllT2() throws Exception{
		//lazy=false
		t3 = (T3)testService.findObject(T3.class, id);
//		t2l = t3.getT2();
//		t2s = t3.getT2();
		getRequest().setAttribute("tlist", t2s);
		return SUCCESS;
	}
	
	public T2 getT2() {
		return t2;
	}

	public void setT2(T2 t2) {
		this.t2 = t2;
	}

	public T3 getT3() {
		return t3;
	}

	public void setT3(T3 t3) {
		this.t3 = t3;
	}

	public List<T2> getTtl() {
		return ttl;
	}

	public void setTtl(List<T2> ttl) {
		this.ttl = ttl;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Set<T2> getT2s() {
		return t2s;
	}

	public void setT2s(Set<T2> t2s) {
		this.t2s = t2s;
	}

}
